﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Common;

public partial class Business_Business_Add : System.Web.UI.Page
{
    protected string MenuType
    {
        get { return string.IsNullOrEmpty(Request["MenuType"]) ? "" : Request["MenuType"].ToString(); }
    }

    protected int UserType
    {
        get { return Convert.ToInt32(Session["UserType"]); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserKey"] == null)
        {
            Response.Redirect("../index.aspx");
        }

        if (!this.IsPostBack)
        {
            List<NewsType> NewsTypeList = new Sys_Para().SelectNewsType();
            ClsID.DataSource = NewsTypeList;   
            ClsID.DataTextField = "TypeName";
            ClsID.DataValueField = "TypeKey";
            ClsID.DataBind();

            if (UserType == 1)
                TR_MUserKey.Visible = false;
            else
                TR_MUserKey.Visible = true;

            TechOneD.Attributes.Add("onfocus", "selectDate(this)");
            TechOneD.Attributes.Add("onpropertychange", "setDefaultDate(this)");
            TechTwoD.Attributes.Add("onfocus", "selectDate(this)");

            IList<Sys_Account>  UserList = new Account().GetNotAccount(1);
            foreach (Sys_Account account in UserList)
                MUserKey.Items.Add(new ListItem("◆ "+account.RealName,account.UserKey.ToString()));


            if (UserType == 1)   //客户
            {
                UserID.Value = Session["UserKey"].ToString();

                Sys_Account user = new Account().GetAccountByKey(Convert.ToInt32(Session["UserKey"]));
                UserCompany.Text = user.CompanyName;
                UserName.Text = user.RealName;
                UserNameTEL.Text = user.Tel;
            }
            else
                UserID.Value = "";

            
        }
    }

    //添加
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Dat_News news;

        string vallist = News.GetChkListValue(ClsID);

        if ( vallist.Contains("6") ) //报价
        {  
            Decimal? de = null;
            news = new Dat_News
           {
               DanNum = System.DateTime.Now.ToString("yyyyMMddHHmm"),
               DanNumLID = 0,
               RootID = 0,
               ClsID = vallist,
               UserKey = Convert.ToInt32(Session["UserKey"]),
               MUserKey = Convert.ToInt32(MUserKey.SelectedValue),
               //Topic = Topic.Text.Trim(),
               Content_Cus = Content_Cus.Text.Trim(),
               CreateTime = System.DateTime.Now,
               UpdateTime = System.DateTime.Now,
               TechOneTime = Convert.ToDateTime(TechOneD.Text.Trim() + " " + TechOneT_H.Text + ":" + TechOneT_M.Text + ":00"),
               TechTwoTime = Convert.ToDateTime(TechTwoD.Text.Trim() + " " + TechTwoT_H.Text + ":" + TechTwoT_M.Text + ":00"),
               UserID = Convert.ToInt32(UserID.Value),
               UserCompany = new Account().GetAccountCompanyName(Convert.ToInt32(UserID.Value)),
               UserName = UserName.Text.Trim(),
               UserNameTEL = UserNameTEL.Text.Trim(),
               Type = 1,
               Quote_StockType = Quote_StockType.Value,
               Quote_StockID = Convert.ToInt32(Quote_StockID.Value),
               Quote_IsQuote = Quote_IsQuote.SelectedValue,
               Quote_Price = Quote_Price.Text.Trim() == "" ? de : Convert.ToDecimal(Quote_Price.Text.Trim())
           };
        }
        else if ( vallist.Contains("4") )  //送/取
        {
            news = new Dat_News
            {
                DanNum = System.DateTime.Now.ToString("yyyyMMddHHmm"),
                DanNumLID = 0,
                RootID = 0,
                ClsID = vallist,
                UserKey = Convert.ToInt32(Session["UserKey"]),
                MUserKey = Convert.ToInt32(MUserKey.SelectedValue),
                //Topic = Topic.Text.Trim(),
                Content_Cus = Content_Cus.Text.Trim(),
                CreateTime = System.DateTime.Now,
                UpdateTime = System.DateTime.Now,
                TechOneTime = Convert.ToDateTime(TechOneD.Text.Trim() + " " + TechOneT_H.Text + ":" + TechOneT_M.Text + ":00"),
                TechTwoTime = Convert.ToDateTime(TechTwoD.Text.Trim() + " " + TechTwoT_H.Text + ":" + TechTwoT_M.Text + ":00"),
                UserID = Convert.ToInt32(UserID.Value),
                UserCompany = new Account().GetAccountCompanyName(Convert.ToInt32(UserID.Value)),
                UserName = UserName.Text.Trim(),
                UserNameTEL = UserNameTEL.Text.Trim(),
                Type = 1,
                Fetch_FetchType = Fetch_FetchType.Value,
                Fetch_FetchID = Convert.ToInt32(Fetch_FetchID.Value),
                Fetch_DanNum = Fetch_DanNum.Text
            };
        }
        else
        {
            news = new Dat_News
            {
                DanNum = System.DateTime.Now.ToString("yyyyMMddHHmm"),
                DanNumLID = 0,
                RootID = 0,
                ClsID = vallist,
                UserKey = Convert.ToInt32(Session["UserKey"]),
                MUserKey = Convert.ToInt32(MUserKey.SelectedValue),
                //Topic = Topic.Text.Trim(),
                Content_Cus = Content_Cus.Text.Trim(),
                CreateTime = System.DateTime.Now,
                UpdateTime = System.DateTime.Now,
                TechOneTime = Convert.ToDateTime(TechOneD.Text.Trim() + " " + TechOneT_H.Text + ":" + TechOneT_M.Text + ":00"),
                TechTwoTime = Convert.ToDateTime(TechTwoD.Text.Trim() + " " + TechTwoT_H.Text + ":" + TechTwoT_M.Text + ":00"),
                UserID = Convert.ToInt32(UserID.Value),
                UserCompany = new Account().GetAccountCompanyName(Convert.ToInt32(UserID.Value)),
                UserName = UserName.Text.Trim(),
                UserNameTEL = UserNameTEL.Text.Trim(),
                Type = 1,
            };
        }



        if (new News().AddNews(news))
        {
            if (UserType == 2 || UserType == 3 || UserType == 4 )
                WebUtility.JsAlertToGo("预约成功", "Business.aspx?menutype=133&type=1,2,4,5");
            else
                WebUtility.JsAlertToGo("预约成功", "Business.aspx?menutype=113");
        }
    }

    //任务类型切换
    protected void ClsID_SelectedIndexChanged(object sender, EventArgs e)
    {
        string vallist = News.GetChkListValue(ClsID);

        if (vallist.Contains("4") && vallist.Contains("6"))
        {
            Msg.Text = "送/取服务和报价不能同时选中";
            News.ChkListClear(ClsID);
            P_Quote.Visible = false;
            P_Fetch.Visible = false;
        }
        else
        {
            Msg.Text = "";

            if (vallist.Contains("6"))   //报价
                P_Quote.Visible = true;
            else
                P_Quote.Visible = false;

            if (vallist.Contains("4"))  //送/取
                P_Fetch.Visible = true;
            else
                P_Fetch.Visible = false;
        }
    }

    //报价切换
    protected void Quote_IsQuote_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Quote_IsQuote.SelectedValue == "已报价")
        {
            LB_Quote_Price.Visible = true;
            Quote_Price.Visible = true;
        }
        else
        {
            LB_Quote_Price.Visible = false;
            Quote_Price.Visible = false;
        }
    }
}